#include "gamecontrol.h"
#include "gamedefine.h"
#include"gamethings.h"


GameControl* GameControl::instance= nullptr;
GameControl::GameControl()
{

}

void GameControl::GameInit()
{
    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);

    LoadStartScene();//开始场景初始化

    //定时器初始化
    mplayerMoveTimer = new QTimer(this);
    mEnemyfallingCreateTimer = new QTimer(this);
    mEnemyfallingMoveTimer =new QTimer(this);


    //绑定定时器处理函数
    connect(mplayerMoveTimer,&QTimer::timeout,this,&GameControl::playermove);
    connect(mEnemyfallingCreateTimer,&QTimer::timeout,this,&GameControl::Createfalling);
    connect(mEnemyfallingMoveTimer,&QTimer::timeout,[this](){
        //掉落物移动
        for(auto enemyfalling : mEnemyfallingList)
        {
            enemyfalling->EnemyfallingMove();
        }

        //碰撞检测
        //Collision();
    });

}

void GameControl::LoadStartScene()
{
    mStartScene.setSceneRect(QRect(0,0,1024,1024));//游戏开始场景
    //开始场景初始化
    mStartScene.addPixmap(QPixmap(":/start/images/startscene.png"));

    auto startBtn=new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->resize(400,100);
    startBtn->setIcon(QIcon(":/start/images/startbutton.png"));
    startBtn->setIconSize(QSize(400,100));
    startBtn->move(724,700);
    //开始游戏点击
    connect(startBtn,&QToolButton::clicked,[this](){


        //加载游戏场景
        this->LoadGameScene();
        //开始游戏
        this->GameStart();

    });

    mStartScene.addWidget(startBtn);

    //设置当前场景为开始场景
    mGameView.setScene(&mStartScene);
    mGameView.show();
}

void GameControl::LoadGameScene()
{
    mGameScene.setSceneRect(QRect(0,0,1024,1024));//游戏进行场景
    mbackground.setPixmap(QPixmap(":/background/images/background.png"));
    mmyplayer.setPixmap(QPixmap(":/player/images/player.png"));

    //图片元素添加到场景
    mGameScene.addItem(&mbackground);
    mGameScene.addItem(&mmyplayer);







    //设置当前场景为游戏场景
    mGameView.setScene(&mGameScene);
    mGameView.show();
}

void GameControl::GameStart()
{
    //开启定时器
    mplayerMoveTimer->start(GameDefine::playerMoveTime);
    mEnemyfallingCreateTimer->start(GameDefine::EnemyfallingCreateTime);
    mEnemyfallingMoveTimer->start(GameDefine::EnemyfallingMoveTime);
}

void GameControl::GameOver()
{
    //结束逻辑
}

void GameControl::playermove()
{
    for(int keyCode : mKeyList)
    {
        switch (keyCode)
        {
        case   Qt::Key_A:mmyplayer.moveBy(-1*mmyplayer.Movespeed(),0);break;
        case   Qt::Key_D:mmyplayer.moveBy(1*mmyplayer.Movespeed(),0);break;
        }
    }

    //边界判断
    if (mmyplayer.x()<0)
    {
        mmyplayer.setX(0);
    }
    if (mmyplayer.y()<800)
    {
        mmyplayer.setY(800);
    }
    if (mmyplayer.x()>800)
    {
        mmyplayer.setX(800);
    }
    if (mmyplayer.y()>800)
    {
        mmyplayer.setY(800);
    }
}



void GameControl::Createfalling()
{
    QPixmap pixmap(":/falling/images/bomb.png");
    int randX=rand()%(800);
    Enemyfalling *enemyfalling =new Enemyfalling(QPoint(randX,-224),pixmap);

    //添加到场景
    mGameScene.addItem(enemyfalling);

    //添加到管理器
    mEnemyfallingList.append(enemyfalling);
}
